package Communicate;

/**
 * @author wuhan
 * @date 2019/9/19 14:22
 */
class Res {
    private String name;
    private String sex;
    boolean flag=false;
    public Res() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

class Out implements Runnable{
    private Res r;
    public Out(Res r) {
        this.r=r;
    }

    @Override
    public void run() {
        while (true){
        synchronized (r){
            if (!r.flag){
                try {
                    r.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(r.getName()+r.getSex());
            r.flag=false;
            r.notify();
            }

        }
    }


}

class In implements Runnable{
    private Res r;
    public In(Res r) {
        this.r=r;
    }

    @Override
    public void run() {
        int x=0;
        while (true){
            synchronized (r){
                if (r.flag){
                    try {
                        r.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                    if (x==0){
                        r.setName("吴焓");
                        r.setSex("男");
                    }
                    else {
                        r.setName("ljy");
                        r.setSex("nv");
                    }
                    x=(x+1)%2;
                    r.flag=true;
                    r.notify();
                }
            }
        }
}




public class InOutDemo {


    public static void main(String[] args) {
        Res res = new Res();
        Thread thread0=new Thread(new In(res));
        Thread thread1=new Thread(new Out(res));
        thread0.start();thread1.start();
    }

}
